<template>
    <section>
        <!--高级查询工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.keyword" placeholder="请输入菜单名称" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="queryDepts">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd()">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="emps" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="菜单名称" width="150" sortable>
                <!-- :formatter="formatSex" -->
            </el-table-column>
            <el-table-column prop="component" label="菜单构成" width="200" sortable>
            </el-table-column>
            <el-table-column prop="url" label="菜单地址" width="200" sortable>
            </el-table-column>
            <el-table-column prop="icon" label="icon" width="150" sortable>
            </el-table-column>
            <el-table-column prop="index" label="菜单索引" width="150" sortable>
            </el-table-column>
            <el-table-column prop="parentname" label="所属菜单" width="150" sortable>
            </el-table-column>
            <el-table-column prop="intro" label="intro" width="150" sortable>
            </el-table-column>

            <el-table-column label="操作" >
                <!-- scope当前行 -->
                <template scope="scope">
                    <el-button size="small" @click=" handlesave(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--分页工具条-->
        <el-col :span="24" class="toolbar">
            <!-- :disabled="this.sels.length===0" 判断选择数据 disabled true禁用  false启用-->
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <!--
              style="float:right; 靠右展示
            -->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 15, 30]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    style="float:right;">
            </el-pagination>
        </el-col>

        <!--
            编辑界面
            visible.sync 模态框是否显示 默认为false
            :model 绑定表单数据
            prop 用于做表单验证 配合:rules="saveFormRules"使用
        -->
        <el-dialog :title="saveTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false">
            <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
                <!-- prop -->
                <el-form-item label="菜单名称" prop="name">
                    <el-input v-model="saveForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="菜单构成" prop="component">
                    <el-input v-model="saveForm.component" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="菜单地址" prop="url">
                    <el-input v-model="saveForm.url" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="icon" prop="icon">
                    <el-input v-model="saveForm.icon" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="所属菜单" prop="parentId">

                    <!--
                         v-for: 循环
                         :key 用于去重 必须唯一 如果有相同的 就会将两条数据变成一条数据
                         :label 对应下拉列表左边的值
                         :value 提交给v-model赋值
                        -->
                    <el-select v-model="saveForm.parentId" placeholder="请选择">
                        <el-option
                                v-for="item in parentname"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            <span style="float: left">{{ item.name }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.index }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="intro" prop="intro">
                    <el-input v-model="saveForm.intro" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="saveFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
            </div>
        </el-dialog>


    </section>
</template>

<script>
    export default {
        data() {
            return {
                //分页
                total: 0,//总条数
                currentPage: 1,//第几页
                pageSize: 10,//每页条数

                //列表展示
                emps: [],

                //高级查询
                filters: {

                    keyword: '',
                },

                //批量删除
                sels: [],//列表选中列

                //保存界面是否显示
                saveFormVisible: false,

                //保存忙等
                saveLoading: false,
                //保存的标题 新增或者标题
                saveTitle:'',
                //获取列表忙等
                listLoading: false,
                //表单的验证
                saveFormRules: {
                    //required 必须传入 message:提示信息  trigger:焦点
                    name: [
                        { required: true, message: '请输入菜单名称', trigger: 'blur' }
                    ]
                },
                //保存界面数据
                saveForm: {
                    name: null,
                    component: null,
                    url: null,
                    icon: null,
                    index: null,
                    parentId: null,
                    intro: null,
                    state: null,
                },
                //上级菜单列表
                parentname: [],
                //部门树
                tree:[],

            }
        },
        methods: {
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getmenus();//刷新页面
            },
            handleSizeChange(val){
                this.pageSize = val;
                this.getmenus();
            },
            queryDepts(){
                this.currentPage = 1;
                this.getmenus();
            },
            //获取用户列表
            getmenus() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword:this.filters.keyword,
                    state:this.filters.state,
                }
                console.log(para)
                this.listLoading = true;//开启忙等
                //获得用户数据
                this.$http.post("/menu/list",para).then(res=>{
                    this.emps = res.data.data
                    this.total = res.data.total
                    this.listLoading = false;
                })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => { //在删除弹框时 点击确定
                    //开启忙等
                    this.listLoading = true;

                    this.$http.delete("/menu/"+row.id).then(res=>{
                        //console.log(res.data.msg)
                        //关闭忙等
                        this.listLoading = false;
                        //判断后端传回来的删除状态
                        if(res.data.success){
                            //弹出提示框
                            this.$message({
                                message: res.data.msg,
                                type: 'success'
                            });
                            //删除成功刷新列表
                            this.currentPage = 1;
                            this.getmenus();
                        }else {
                            this.$message({
                                message: res.data.msg,
                                //删除失败弹出警告框
                                type: 'warning'
                            });
                        }
                        //NProgress.done();

                    })
                }).catch(() => { //在删除弹框时 点击取消

                });
            },
            //显示编辑界面
            handlesave: function (index, row) {
                if(this.$refs['saveForm']){
                    this.$refs['saveForm'].resetFields();//重置表单验证
                }
                this.saveTitle = "编辑"
                //是否显示弹出框
                this.saveFormVisible = true;
                //给表单赋值
                this.saveForm = Object.assign({}, row);

            },
            //显示新增界面
            handleAdd: function () {
                if(this.$refs['saveForm']){
                    this.$refs['saveForm'].resetFields();//重置表单验证
                }
                this.saveTitle = "新增"
                this.saveForm = {
                    id:null,
                    sn: '',
                    name: '',
                    state: 1,
                    managerId: null,
                    parentId: null,
                };
                //是否显示弹出框
                this.saveFormVisible = true;
            },
            //保存提交
            saveSubmit: function () {
                //手动触发提示
                this.$refs.saveForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '温馨提示', {}).then(() => {
                            //开启忙等
                            this.saveLoading = true;
                            //因为双向绑定
                            let para = Object.assign({}, this.saveForm); //克隆
                            //判断是否有父部门
                            if(para.parentId){

                                if(typeof para.parentId=="object"){
                                    para.parentId = para.parentId[para.parentId.length-1];
                                }
                            }
                            this.$http.put("/menu",para).then(res=>{
                                //console.log(res)
                                if(res.data.success){
                                    //忙等
                                    this.saveLoading = false;
                                    this.$message({
                                        message: res.data.msg,
                                        type: 'success'
                                    });
                                    //重置表单验证
                                    this.$refs['saveForm'].resetFields();
                                    //关闭保存页面
                                    this.saveFormVisible = false;
                                    this.getmenus();
                                }else {
                                    this.$message({
                                        message: res.data.msg,
                                        type: 'warning'
                                    });
                                    this.saveLoading = false;
                                }
                            })
                        });
                    }
                });
            },

            //监听列表多选框，绑定事件，如果勾选了就获取当前的值，多条
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                //获得sels中每一条数据的id
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    //开启忙等
                    this.listLoading = true;
                    this.$http.patch("/menu/del",ids).then(res=>{
                        this.listLoading = false;
                        if (res.data.success){
                            this.$message({
                                message: res.data.msg,
                                type: 'success'
                            });
                            this.currentPage = 1;
                            this.getmenus();
                        }else {
                            this.$message({
                                message: res.data.msg,
                                type: 'warning'
                            });
                        }
                    })
                }).catch(() => {

                });
            },
            //加载父级菜单
            getParent: function () {
                this.$http("/menu/parent").then(res=>{
                    console.log(res)
                    this.parentname = res.data.data
                })
            },

        },

        //项目初始化时加载
        mounted() {
            this.getmenus();
            this.getParent();
        }
    }

</script>

<style scoped>

</style>